package com.edu.boot.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.boot.common.api.CommonResult;
import com.edu.boot.config.UserContext;
import com.edu.boot.entity.News;
import com.edu.boot.entity.SystemAdvice;
import com.edu.boot.enums.AdviceStatus;
import com.edu.boot.enums.UserTypeEnum;
import com.edu.boot.service.SystemAdviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 系统优化建议
 */
@RestController
@RequestMapping("/systemAdvice")
public class SystemAdviceController {
    
    @Autowired
    private SystemAdviceService systemAdviceService;
    
    // 查询全部
    @GetMapping("/list")
    public CommonResult<List<SystemAdvice>> getAll() {
        List<SystemAdvice> list = systemAdviceService.list();
        return CommonResult.success(list);
    }
    
    // 根据id查询
    @GetMapping("/get/{id}")
    public CommonResult<SystemAdvice> getById(@PathVariable Integer id) {
        return CommonResult.success(systemAdviceService.getById(id));
    }
    
    // 新增
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody SystemAdvice systemAdvice) {
        systemAdvice.setAdviceStatus(AdviceStatus.DCL.getCode());
        systemAdvice.setCreateTime(DateUtil.date());
        systemAdvice.setUid(UserContext.getUserId());
        systemAdviceService.save(systemAdvice);
        return CommonResult.success("添加成功");
    }
    
    // 更新
    @PostMapping("/update")
    public CommonResult<String> update(@RequestBody SystemAdvice systemAdvice) {
        SystemAdvice oldSystemAdvice = systemAdviceService.getById(systemAdvice.getId());
        if (Objects.isNull(oldSystemAdvice) || AdviceStatus.YCL.getCode().equals(oldSystemAdvice.getAdviceStatus())) {
            return CommonResult.failed("该建议已处理，无法进行更改！");
        }
        systemAdviceService.updateById(systemAdvice);
        return CommonResult.success("更新成功");
    }
    
    // 删除
    @GetMapping("/delete/{id}")
    public CommonResult<String> delete(@PathVariable Integer id) {
        SystemAdvice systemAdvice = systemAdviceService.getById(id);
        if (Objects.isNull(systemAdvice) || AdviceStatus.YCL.getCode().equals(systemAdvice.getAdviceStatus())) {
            return CommonResult.failed("该建议已处理，无法删除！");
        }
        systemAdviceService.removeById(id);
        return CommonResult.success("删除成功");
    }
    
    // 模糊查询（例如按标题）
    @GetMapping("/page")
    public CommonResult getAllNews(@RequestParam(name = "currentpage") Integer current,
                                   @RequestParam(name = "pagesize") Integer size,
                                   @RequestParam(name = "adviceTitle", required = false) String adviceTitle) {
        Page<SystemAdvice> page = new Page<>(current, size);
        LambdaQueryWrapper<SystemAdvice> queryWrapper = new LambdaQueryWrapper<>();
    
        // 仅当不是管理员时，才添加 uid 限制
        if (!UserContext.getPid().equals(UserTypeEnum.admin.getCode())) {
            queryWrapper.eq(SystemAdvice::getUid, UserContext.getUserId());
        }
    
        // 可选的搜索条件
        if (Objects.nonNull(adviceTitle) && !adviceTitle.isEmpty()) {
            queryWrapper.like(SystemAdvice::getAdviceTitle, adviceTitle);
        }
    
        // 排序
        queryWrapper.orderByDesc(SystemAdvice::getCreateTime);
    
        Page<SystemAdvice> resultPage = systemAdviceService.page(page, queryWrapper);
        if(CollectionUtil.isNotEmpty(resultPage.getRecords())){
            resultPage.getRecords().stream().forEach(systemAdvice -> {
                systemAdvice.setAdviceStatusMsg(AdviceStatus.getByCode(systemAdvice.getAdviceStatus()));
            });
        }
        
        return CommonResult.success(resultPage);
    }
    
    // (企业端进行处理)优化建议
    @GetMapping("/adviceDeal")
    public CommonResult adviceDeal(@RequestParam Integer id,
                                   @RequestParam String adviceResult) {
        SystemAdvice systemAdvice = new SystemAdvice();
        systemAdvice.setId(id);
        systemAdvice.setAdviceResult(adviceResult);
        systemAdvice.setAdviceStatus(AdviceStatus.YCL.getCode());
        systemAdvice.setAdviceTime(DateUtil.date());
        systemAdviceService.updateById(systemAdvice);
        return CommonResult.success();
    }
}
